package com.titan.demo.camel.utils;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public abstract class RunCamel {
	public void run(RouteBuilder...builder) {
        CamelContext context = null;
        ProducerTemplate producer = null;
        ConsumerTemplate consumer = null;
        try {
            context = new DefaultCamelContext();
            context.setTracing(true);
            for (int i = 0; i < builder.length; i++) {
            	context.addRoutes(builder[i]);
			}
            
            producer = context.createProducerTemplate();
            producer.start();
            consumer = context.createConsumerTemplate();
            consumer.start();
            context.start();
            
        	execute(context, producer, consumer);
        } catch(Exception ex) {
        	ex.printStackTrace();
        } finally {
            CamelUtils.close(consumer);
            CamelUtils.close(producer);
            CamelUtils.close(context);
        }		
	}	

	
	protected abstract void execute(CamelContext context
			, ProducerTemplate producer
			, ConsumerTemplate consumer
			) throws Exception;
}
